<?php
namespace Home\Controller;

class DeptController extends CommonController {
     
//     public function index(){
//         if (isset($_SESSION['username'])){
//             $this->display();
//         }else{
//             header("location: ". __ROOT__."/index.php/login");
//         }
//     }
    
    public function staff(){
         $limit=$_GET['limit'];
         $start=$_GET['start'];
         $pagesize=$_GET['page']; 
    
        $staff = D('staff');
        //         $count = $staff->count();
        if('' !== I('id')){
           // var_dump(I('id'));
            $department = I('id');
            $count = $staff->where('department='.$department)->count();
            $department = I('id');
            $deptname = I('name');
            //         $data['total'] = $count;
        
            $list = $staff->where("department=$department")->select();
        
            $data['data']=$list;
            $data['deptname']=$deptname;
            $data['total'] = $count;
            $data['deptid']=$department;
            $this->ajaxReturn($data);
        }
    }

    //部门信息列表数据展示
    public function select(){
        
        $limit=$_GET['limit'];
        $start=$_GET['start'];
        $pagesize=$_GET['page'];
        
        $dpet = D('dept');
        $count = $dpet->count();       
        $data['total'] = $count;
        
        //$staff = D('staff');
        //$num = $staff->field($field)->group('department')->c
        
        $list = $dpet->field('tbl_dept.id,tbl_dept.name,tbl_dept.manager,count(tbl_staff.department) as num,remark')
        ->join('tbl_staff on tbl_dept.id = tbl_staff.department','left')->group('tbl_dept.id')->limit($start,$limit)->select();

        $data['data']=$list;
        $this->ajaxReturn($data);       
    }
    
    //单个添加员工信息
    public function add(){        
        $dept = D('dept');

        $data['name'] = I('name');
        $data['manager'] = I('manager');
       // $data['num'] = I('num');
        $data['remark'] = I('remark');
       
  
        if (!$dept->create()){
            
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            $result['success'] = false;
            $result['msg'] = $dept->getError();
            exit($this->ajaxReturn($result));
        }else{            
                if($dept->add($data)){
    			$result['success']=true;
    			$result['msg']='保存成功';
    		}else{
    			$result['success']=false;
    			$result['msg']='保存失败';
    		}
    		echo $this->ajaxReturn($result);
        }       
    }
                       
    //部门列表下拉框信息提供
    public function deptSelect(){
       $dept = D('dept');
        
       $list =  $dept->field('id,name')->group('name')->select();
       
       $this->ajaxReturn($list);
    }   
    
    //删除选中的数据
    public function delete(){
        $data=I('data');
        $staff = D('dept');
        $map['id']=array('in',$data);
        //echo $post;
        if($staff->where($map)->delete()>0){
            $result['success']=true;
			$result['msg']='删除成功';
        }else{
            $result['success']=false;
			$result['msg']='删除失败';
        }

        $this->ajaxReturn($result);
    }

    //更新数据操作
    public function update(){
        $dept = D('dept');
        $data=I('post.'); 

    /*     if(!$data['id']){
            $uid = $_SESSION['id'];
            $arr = $dept->field('id')->where("uid=$uid")->find();
            $data['id'] = $arr['id'];
        }  */       
       
         
        if($dept->where("id=".$data['id'])->save($data)!==false){
            //$result['data'] = $data;
            $result['success']=true;//返回修改后的状态
			$result['msg']='修改成功';//状态信息
        }else{
            $result['success']=false;
			$result['msg']='修改失败';
        }
        $this->ajaxReturn($result);
    }
    //设置员工部门
    public function setDept(){
        $staff = D('staff');
        $data['department'] = I('post.department');
        $id = I('post.id');
        /*     if(!$data['id']){
         $uid = $_SESSION['id'];
         $arr = $dept->field('id')->where("uid=$uid")->find();
         $data['id'] = $arr['id'];
        }  */
         
         
        if($staff->where("id=".$id)->save($data)!==false){
            //$result['data'] = $data;
            $result['success']=true;//返回修改后的状态
            $result['msg']='修改成功';//状态信息
        }else{
            $result['success']=false;
            $result['msg']='修改失败';
        }
        $this->ajaxReturn($result);
    }
}